package com.dingreading.cloud.subject.service.impl;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.subject.entity.SubjectLessonLog;
import com.dingreading.cloud.subject.entity.table.SubjectLessonLogTableDef;
import com.dingreading.cloud.subject.mapper.SubjectLessonLogMapper;
import com.dingreading.cloud.subject.service.SubjectLessonLogService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-10-13
 */
@Service
public class SubjectLessonLogServiceImpl extends BaseServiceImpl<SubjectLessonLogMapper, SubjectLessonLog, SubjectLessonLogTableDef> implements SubjectLessonLogService {

    @Override
    protected SubjectLessonLogTableDef getTable() {
        return SubjectLessonLogTableDef.subjectLessonLog;
    }

    @Override
    public R<Object> preventDuplicateDownload(String storeUid, String staffUid, String lessonUid, int type) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.staffUid.eq(staffUid))
                .and(table.lessonUid.eq(lessonUid))
                .and(table.type.eq(type))
                .and(table.addTime.likeLeft(DateUtil.getNowDateStr()));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        List<SubjectLessonLog> list = getMapper().selectListByQuery(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            if (list.size() >= 10) {
                return R.fail("今日下载次数已超过10次");
            } else {
                SubjectLessonLog lessonLog = list.get(0);
                if (lessonLog == null) {
                    return R.fail("未查询到记录");
                }
                Date date = DateUtil.addMinutes(lessonLog.getAddTime(), 1);
                if (date.getTime() >= System.currentTimeMillis()) {
                    return R.fail("下载过于频繁，请在1分钟后下载");
                }
            }
        }
        return R.ok();
    }


    @Override
    public R<Object> preventDuplicateDownload(String storeUid, String staffUid, Long subjectId, Long fileId, int type) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.staffUid.eq(staffUid))
                .and(table.subjectsId.eq(subjectId))
                .and(table.fileId.eq(fileId))
                .and(table.fileType.eq(type))
                .and(table.addTime.likeLeft(DateUtil.getNowDateStr()));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        List<SubjectLessonLog> list = getMapper().selectListByQuery(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            if (list.size() >= 10) {
                return R.fail("今日下载次数已超过10次");
            } else {
                SubjectLessonLog lessonLog = list.get(0);
                if (lessonLog == null) {
                    return R.fail("未查询到记录");
                }
                Date date = DateUtil.addMinutes(lessonLog.getAddTime(), 1);
                if (date.getTime() >= System.currentTimeMillis()) {
                    return R.fail("下载过于频繁，请在1分钟后下载");
                }
            }
        }
        return R.ok();
    }

}
